%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  本脚本用于去除高空噪声点。
%  脚本会查找每个格点的邻域，如果邻域均无塔线点则认定该点为噪声
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i = 2:grid_num-1
    for j = 2:grid_num-1
        if grid_map(i,j).class==1
            if grid_map(i,j).fgnum<3 && grid_map(i,j).fgnum>0
                re = lookaround(grid_map,i,j);
                if re==0
                    p = sortrows(grid_map(i,j).points,5,'descend');
                    ind = find(p(:,5),1,'last');
                    for k = 1:ind 
                        points(p(k,4),5)=0;
                    end
                    grid_map(i,j).class = 0;
                    grid_map(i,j).fgnum = 0;
                    
                    grid_map(i,j).points(:,5)=0;
                end
            end
        end
    end
end

                    



function result = lookaround(grid_map,x,y)
    fgn = reshape([grid_map(:,:).fgnum],[100 100]);
    te = fgn>=3;
    result = sum(te(x-1:x+1,y-1));
    result = result + sum(te(x-1:x+1,y+1));
    result = result + te(x-1,y);
    result = result + te(x+1,y);
    result = result>0;
end
